﻿using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Authorization.Infrastructure.Extensions
{
    /// <summary>
    /// 用户管理的扩展
    /// </summary>
    public static class UserManagerExtensions
    {
        /// <summary>
        /// 修改用户密码，无需提供用户当前密码
        /// </summary>
        /// <param name="userManager"></param>
        /// <param name="user">要修改密码的用户对象</param>
        /// <param name="password">修改后的密码</param>
        /// <returns></returns>
        public static async Task<IdentityResult> ChangePasswordAsync<TUser>(this UserManager<TUser> userManager, TUser user, string password) where TUser : IdentityUser
        {
            var type = userManager.GetType();
            var storeProperty = type.GetProperty("Store", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            if (!(storeProperty.GetValue(userManager) is IUserPasswordStore<TUser> store))
            {
                return IdentityResult.Failed(new IdentityError { Code = "NotImplements", Description = "持久化存储器没有实现IUserPasswordStore接口" });
            }
            var passwordHash = userManager.PasswordHasher.HashPassword(user, password);
            await store.SetPasswordHashAsync(user, passwordHash, CancellationToken.None);
            await store.UpdateAsync(user, CancellationToken.None);
            return IdentityResult.Success;
        }
        /// <summary>
        /// 根据明文密码获取加密的密码
        /// </summary>
        /// <param name="userManager"></param>
        /// <param name="password">修改后的密码</param>
        /// <returns></returns>
        public static string GetPassword<TUser>(this UserManager<TUser> userManager, string password) where TUser : IdentityUser
        {
            var passwordHash = userManager.PasswordHasher.HashPassword(null, password);
            return passwordHash;
        }
        /// <summary>
        /// 更改用户的手机号
        /// </summary>
        /// <param name="userManager"></param>
        /// <param name="user">需要更改的用户</param>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        public static async Task<IdentityResult> ChangePhoneAsync<TUser>(this UserManager<TUser> userManager, TUser user, string phone) where TUser : IdentityUser
        {
            var type = userManager.GetType();
            var storeProperty = type.GetProperty("Store", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            if (!(storeProperty.GetValue(userManager) is IUserPhoneNumberStore<TUser> store))
            {
                return IdentityResult.Failed(new IdentityError { Code = "NotImplements", Description = "持久化存储器没有实现IUserPhoneNumberStore接口" });
            }
            await store.SetPhoneNumberAsync(user, phone, CancellationToken.None);
            await store.UpdateAsync(user, CancellationToken.None);
            return IdentityResult.Success;
        }

        /// <summary>
        /// 更改用户的邮箱认证
        /// </summary>
        /// <param name="userManager"></param>
        /// <param name="user">需要更改的用户</param>
        /// <param name="phone">认证</param>
        /// <returns></returns>
        public static async Task<IdentityResult> ChangeEmailConfirmAsync<TUser>(this UserManager<TUser> userManager, TUser user, bool confirmed) where TUser : IdentityUser
        {
            var type = userManager.GetType();
            var storeProperty = type.GetProperty("Store", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            if (!(storeProperty.GetValue(userManager) is IUserEmailStore<TUser> store))
            {
                return IdentityResult.Failed(new IdentityError { Code = "NotImplements", Description = "持久化存储器没有实现IUserEmailStore接口" });
            }
            await store.SetEmailConfirmedAsync(user, confirmed, CancellationToken.None);
            await store.UpdateAsync(user, CancellationToken.None);
            return IdentityResult.Success;
        }

        /// <summary>
        /// 更改用户的手机认证
        /// </summary>
        /// <param name="userManager"></param>
        /// <param name="user">需要更改的用户</param>
        /// <param name="phone">认证</param>
        /// <returns></returns>
        public static async Task<IdentityResult> ChangePhoneConfirmAsync<TUser>(this UserManager<TUser> userManager, TUser user, bool confirmed) where TUser : IdentityUser
        {
            var type = userManager.GetType();
            var storeProperty = type.GetProperty("Store", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            if (!(storeProperty.GetValue(userManager) is IUserPhoneNumberStore<TUser> store))
            {
                return IdentityResult.Failed(new IdentityError { Code = "NotImplements", Description = "持久化存储器没有实现IUserPhoneNumberStore接口" });
            }
            await store.SetPhoneNumberConfirmedAsync(user, confirmed, CancellationToken.None);
            await store.UpdateAsync(user, CancellationToken.None);
            return IdentityResult.Success;
        }
    }
}
